package ec.atreasure.com.core.wechat;

import android.app.Activity;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import ec.atreasure.com.core.app.Atreasure;
import ec.atreasure.com.core.app.ConfigType;
import ec.atreasure.com.core.wechat.callback.IWechatLoginInCallback;

/**
 * Description :调起微信登录业务逻辑
 * Created by wangjin on 2019/6/29.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class AtreasureWechat {
    public static final String APP_ID = Atreasure.getConfiguration(ConfigType.WE_CHAT_APP_ID);

    public static final String APP_SECRET = Atreasure.getConfiguration(ConfigType.WE_CHAT_APP_SECRET);

    private final IWXAPI WXAPI;

    private IWechatLoginInCallback wechatLoginInCallback;

    private AtreasureWechat() {
        final Activity activity = Atreasure.getConfiguration(ConfigType.ACTIVITY);
        WXAPI = WXAPIFactory.createWXAPI(activity, APP_ID, true);
        WXAPI.registerApp(APP_ID);
    }

    private static final class Holder {
        private static final AtreasureWechat INSTANCE = new AtreasureWechat();
    }

    public static final AtreasureWechat getInstance() {
        return Holder.INSTANCE;
    }

    public final IWXAPI getWXAPI() {
        return WXAPI;
    }

    public final void loginIn() {
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "random_state";
        WXAPI.sendReq(req);
    }

    public AtreasureWechat onLoginSuccess(IWechatLoginInCallback callback) {
        this.wechatLoginInCallback = callback;
        return this;
    }

    public IWechatLoginInCallback getWechatLoginInCallback() {
        return this.wechatLoginInCallback;
    }
}
